package old.ships;

import java.util.*;

//coordinates movements of a group of ships
//coordinates defensive fire of a group of ships

public class Formation extends CombatObject {
	
	private ArrayList<CombatShip> shipRoster;  //List of ships in current formation and subject to this formation.
	private int rosterCount = 0;  //Total number of ships on roster
	private ArrayList<MapCoordinate> wayptList;  //List of map coordinates that formation will move toward
	
	private FormationAbstract formProfile;
	private Navigator navigator;
	public Communicator communicator;
	private FormationExecutor executor;
	
	public Formation(CombatShip[] ships, MapCoordinate[] coords, FormationAbstract form)
	{
		shipRoster = new ArrayList<CombatShip>();
		wayptList = new ArrayList<MapCoordinate>();
		
		formProfile = form;
		navigator = new Navigator();
		communicator = new Communicator(CombatObjectEnum.FORMATION_AI, this);
		executor = new FormationExecutor(CombatObjectEnum.FORMATION_AI, this);
		
		//copy all ships in new formation to ship roster
		for (CombatShip currShip : ships)
		{shipRoster.add(currShip); rosterCount++;}
		
		//copy all waypoints for new formation to wayptList
		for (MapCoordinate currCoord: coords)
		{wayptList.add(currCoord);}		
	}
	
	/*public void addMove(MapCoordinate coords)
	{
		wayptList.clear();
		wayptList.add(coords);		
	}*/
	
	public void addMove(Collection<MapCoordinate> coords)
	{
		wayptList.clear();
		wayptList.addAll(coords);
	}
	
	/*public void addWaypt(MapCoordinate coords)
	{
		wayptList.add(coords);
	}*/
	
	public void addWaypt(Collection<MapCoordinate> coords)
	{
		for (MapCoordinate thisCoord:coords)
		{
			int index = wayptList.indexOf(thisCoord);
			wayptList.remove(index);
		}
	}
	
	/*public void remWaypt(MapCoordinate coords)
	{
		int index = wayptList.indexOf(coords);
		wayptList.remove(index);
	}*/
	
	public void remWaypt(Collection<MapCoordinate> coords)
	{
		for (MapCoordinate thisCoord: coords)
		{
			int index = wayptList.indexOf(thisCoord);
			wayptList.remove(index);
		}
	}
	
	/*public void addShip(CombatShip ship)
	{
		shipRoster.add(ship);
	}*/
	
	public void addShip(Collection<CombatShip> ships)
	{
		shipRoster.addAll(ships);
	}
	
	/*public void remShip(CombatShip ship)
	{
		int index = shipRoster.indexOf(ship);
		shipRoster.remove(index);
		rosterCount--;
	}*/
	
	public void remShip(Collection<CombatShip> ships)
	{
		for (CombatShip thisShip: ships)
		{
			int index = shipRoster.indexOf(thisShip);
			shipRoster.remove(index);
			rosterCount--;
		}
	}
	
	//act on next message in communicator's messageQueue
	public void executeMessage()
	{
		executor.go();
	}
	
	
	
	

}
